package com.boot.hospital.score.core.controller;


import com.boot.hospital.score.common.enums.ResultCodeEnum;
import com.boot.hospital.score.common.enums.ReviewType;
import com.boot.hospital.score.common.util.DateUtils;
import com.boot.hospital.score.common.wrapper.ResponseWrapper;
import com.boot.hospital.score.core.service.ReviewService;
import com.boot.hospital.score.dao.entity.Review;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/review")
@Slf4j
@PropertySources(value = {
        @PropertySource(value = {"classpath:important.properties", "classpath:application.properties"}, encoding = "utf-8")
})
@EnableAutoConfiguration
public class ReviewController {

    @Resource
    private ReviewService reviewService;

//    @ApiOperation("查询所有评审记录")
//    @GetMapping(value = "/selectAll")
//    public ResponseWrapper<Object> selectAll() {
//        ResponseWrapper<Object> responseWrapper = new ResponseWrapper<>();
//        List<Review> reviewList = reviewService.selectAll();
//        responseWrapper.setData(reviewList);
//        return responseWrapper;
//    }

    @ApiOperation("添加评审记录")
    @PostMapping(value = "/addReview")
    public ResponseWrapper<Object> addReview(@RequestParam Map<String, String> params) {
        ResponseWrapper<Object> responseWrapper = new ResponseWrapper<>();
        try {
            Review review = new Review();

            review.setReviewName(params.get("reviewName"));
            review.setReviewType(params.get("reviewType"));
            review.setDuration(Integer.valueOf(params.get("duration")));
            review.setIsEnabled(params.get("isEnabled"));
            review.setDescription(params.get("description"));
            review.setMemo(params.get("memo"));

            if (review.getReviewName().equals("")) {
                responseWrapper.setCode(String.valueOf(ResultCodeEnum.FAIL.getCode()));
                responseWrapper.setMsg("评审名称为空");
                return responseWrapper;
            }
            if (review.getReviewType().equals("")) {
                responseWrapper.setCode(String.valueOf(ResultCodeEnum.FAIL.getCode()));
                responseWrapper.setMsg("分值类型为空为空");
                return responseWrapper;

            }
            if (review.getIsEnabled().equals("")) {
                responseWrapper.setCode(String.valueOf(ResultCodeEnum.FAIL.getCode()));
                responseWrapper.setMsg("是否启用为空");
                return responseWrapper;
            }
            if (review.getReviewType().equals("1")) {
                review.setReviewType(ReviewType.GRADE.getName());
            } else if (review.getReviewType().equals("0")) {
                review.setReviewType(ReviewType.HUNDRED_MARK.getName());
            } else {
                responseWrapper.setCode(String.valueOf(ResultCodeEnum.FAIL.getCode()));
                responseWrapper.setMsg("分值类型错误");
                return responseWrapper;

            }

            review.setCreateDate(DateUtils.toDate(LocalDateTime.now()));
            int result = reviewService.insert(review);
            if (result > 0) {
                responseWrapper.setCode(String.valueOf(ResultCodeEnum.SUCCESS.getCode()));
                responseWrapper.setMsg(ResultCodeEnum.SUCCESS.getName());
            } else {
                responseWrapper.setCode(String.valueOf(ResultCodeEnum.FAIL.getCode()));
                responseWrapper.setMsg(ResultCodeEnum.ADD_FAIL.getName());
            }
        } catch (Exception e) {
            log.error("添加评审记录失败：{}", ExceptionUtils.getStackTrace(e));
            responseWrapper.setMsg(ExceptionUtils.getStackTrace(e));
            responseWrapper.setCode(String.valueOf(ResultCodeEnum.FAIL.getCode()));
        }
        return responseWrapper;
    }
    @Transactional(rollbackFor = Exception.class)
    @ApiOperation("根据IDs删除评审记录")
    @DeleteMapping(value = "/deleteReviews")
    public ResponseWrapper<Object> deleteReviewsByIds(@RequestBody List<Integer> reviewIds) {
        ResponseWrapper<Object> responseWrapper = new ResponseWrapper<>();
        try {
            if (reviewIds.size() <= 0) {
                responseWrapper.setCode(String.valueOf(ResultCodeEnum.FAIL.getCode()));
                responseWrapper.setMsg("删除评审记录id为空");
                return responseWrapper;
            }
            int result = reviewService.deleteReviewsByIds(reviewIds);
            if (result > 0) {
                responseWrapper.setCode(String.valueOf(ResultCodeEnum.SUCCESS.getCode()));
                responseWrapper.setMsg(ResultCodeEnum.SUCCESS.getName());
            } else {
                responseWrapper.setCode(String.valueOf(ResultCodeEnum.FAIL.getCode()));
                responseWrapper.setMsg("数据库删除失败");
            }
        } catch (Exception e) {
            log.error("根据IDs删除评审记录失败：{}", ExceptionUtils.getStackTrace(e));
            responseWrapper.setMsg(ExceptionUtils.getStackTrace(e));
            responseWrapper.setCode(String.valueOf(ResultCodeEnum.FAIL.getCode()));
        }
        return responseWrapper;
    }

    @ApiOperation("更新评审记录")
    @PostMapping(value = "/updateReview")
    public ResponseWrapper<Object> updateReview(@RequestParam Map<String, String> params) {
        ResponseWrapper<Object> responseWrapper = new ResponseWrapper<>();
        try {

            Review review = new Review();

            review.setReviewName(params.get("reviewName"));
            review.setReviewType(params.get("reviewType"));
            review.setDuration(Integer.valueOf(params.get("duration")));
            review.setIsEnabled(params.get("isEnabled"));
            review.setDescription(params.get("description"));
            review.setMemo(params.get("memo"));
            review.setReviewId(Integer.valueOf(params.get("id")));
            review.setCreateDate(reviewService.selectById(Integer.valueOf(params.get("id"))).getCreateDate());
            if (review.getReviewId() == null) {
                responseWrapper.setCode(String.valueOf(ResultCodeEnum.FAIL.getCode()));
                responseWrapper.setMsg("评审id为空");
                return responseWrapper;
            }
            if (review.getReviewType().equals("1")) {
                review.setReviewType(ReviewType.GRADE.getName());
            } else if (review.getReviewType().equals("0")) {
                review.setReviewType(ReviewType.HUNDRED_MARK.getName());
            } else {
                responseWrapper.setCode(String.valueOf(ResultCodeEnum.FAIL.getCode()));
                responseWrapper.setMsg("分值类型错误");
                return responseWrapper;

            }
            int result = reviewService.updateReview(review);
            if (result > 0) {
                responseWrapper.setCode(String.valueOf(ResultCodeEnum.SUCCESS.getCode()));
                responseWrapper.setMsg(ResultCodeEnum.SUCCESS.getName());
            } else {
                responseWrapper.setCode(String.valueOf(ResultCodeEnum.FAIL.getCode()));
                responseWrapper.setMsg(ResultCodeEnum.UPDATE_FAIL.getName());
            }
        } catch (Exception e) {
            log.error("更新评审记录失败：{}", ExceptionUtils.getStackTrace(e));
            responseWrapper.setMsg(ExceptionUtils.getStackTrace(e));
            responseWrapper.setCode(String.valueOf(ResultCodeEnum.FAIL.getCode()));
        }
        return responseWrapper;
    }

    @ApiOperation("分页和模糊查询")
    @GetMapping(value = "/getReviews/{page}/{size}")
    public ResponseWrapper<Object> getReviews(@PathVariable Integer page, @PathVariable Integer size, @RequestParam String search) {
        ResponseWrapper<Object> responseWrapper = new ResponseWrapper<>();
        try {
            Map<String, Object> params = new HashMap<>();
            if (search.equals("null") || search.equals("NULL")) {
                params.put("search", "");
            }
            params.put("pageSize", size);
            params.put("offset", page);
            params.put("search", search);

            List<Review> reviewList = reviewService.selectByPageAndSearch(params);
            responseWrapper.setData(reviewList);
            responseWrapper.setCode(String.valueOf(ResultCodeEnum.SUCCESS.getCode()));
            responseWrapper.setMsg(ResultCodeEnum.SUCCESS.getName());
        } catch (Exception e) {
            log.error("分页和模糊查询失败：{}", ExceptionUtils.getStackTrace(e));
            responseWrapper.setMsg(ExceptionUtils.getStackTrace(e));
            responseWrapper.setCode(String.valueOf(ResultCodeEnum.FAIL.getCode()));
        }
        return responseWrapper;
    }

    @ApiOperation("查询基本信息评审记录")
    @GetMapping(value = "/getReviewsBasic")
    public ResponseWrapper<Object> getReviewsBasic(@RequestParam String search) {
        ResponseWrapper<Object> responseWrapper = new ResponseWrapper<>();
        try {
            List<Review> reviewList = reviewService.selectBySearch(search);
            responseWrapper.setData(reviewList);
            responseWrapper.setCode(String.valueOf(ResultCodeEnum.SUCCESS.getCode()));
            responseWrapper.setMsg(ResultCodeEnum.SUCCESS.getName());
        } catch (Exception e) {
            log.error("查询基本信息评审记录：{}", ExceptionUtils.getStackTrace(e));
            responseWrapper.setMsg(ExceptionUtils.getStackTrace(e));
            responseWrapper.setCode(String.valueOf(ResultCodeEnum.FAIL.getCode()));
        }
        return responseWrapper;

    }

    @ApiOperation("查询信息评审记录")
    @GetMapping(value = "/getReviewDetail")
    public ResponseWrapper<Object> getReviewDetail(@RequestParam Integer reviewId) {
        ResponseWrapper<Object> responseWrapper = new ResponseWrapper<>();
        try {
            Review review = reviewService.selectById(reviewId);
            responseWrapper.setData(review);
            responseWrapper.setCode(String.valueOf(ResultCodeEnum.SUCCESS.getCode()));
            responseWrapper.setMsg(ResultCodeEnum.SUCCESS.getName());
        } catch (Exception e) {
            log.error("查询信息评审记录：{}", ExceptionUtils.getStackTrace(e));
            responseWrapper.setMsg(ExceptionUtils.getStackTrace(e));
            responseWrapper.setCode(String.valueOf(ResultCodeEnum.FAIL.getCode()));
        }
        return responseWrapper;

    }
}